#include "FileUpRequest.h"


FileUpRequest::FileUpRequest(void)
{
	m_pkUploadThread = new UploadThread();
}


FileUpRequest::~FileUpRequest(void)
{
}


int FileUpRequest::ResponseAnalysis()
{
	m_pcCopyParse = m_cCopyBuf;

	switch (*m_pcCopyParse)
	{
	case '2':
		if (m_nState == m_kFileUpenum::TYPE)
		{
			send(m_nConn, "PASV\r\n", 6, 0);
			m_nState = m_kFileUpenum::PASV;
		}
		else if (m_nState == m_kFileUpenum::PASV)
		{
			SetDataPort();
			CreateDataConn();
			sprintf(m_cSendBuf, "STOR %s\r\n", m_cFileName);
			send(m_nConn, m_cSendBuf, strlen(m_cSendBuf), 0);
			delete m_pkUploadThread;
			m_pkUploadThread = new UploadThread();
			m_pkUploadThread->SetDataSock(m_nDataConn);
			m_pkUploadThread->SetFilePointer(m_pFile);
			m_pkUploadThread->SetProgressBar(m_pkProgressCtrl);
			m_pkUploadThread->Run();
			m_nState = m_kFileUpenum::NONE;
		}
		break;

	case '4':
		//make Data connection failed handle
		m_nState = m_kFileUpenum::NONE;
		AfxMessageBox(m_cCopyBuf);
		break;

	case '5':
		//make Data connection failed handle
		m_nState = m_kFileUpenum::NONE;
		AfxMessageBox(m_cCopyBuf);
		break;
	}
	return 0;
}

void FileUpRequest::SendSTOR()
{
	send(m_nConn, "TYPE I\r\n", 8, 0);
	m_nState = m_kFileUpenum::TYPE;
}

void FileUpRequest::SetProgressBar(CProgressCtrl *pProCtrl)
{
	m_pkProgressCtrl = pProCtrl;
}